package code.starivan.niuke.jindian;

import java.util.ArrayList;
import java.util.LinkedList;

/**
 * Created by Ivan on 2015/8/29.
 */
public class SetOfStacks {
    public ArrayList<ArrayList<Integer>> setOfStacks(int[][] ope, int size) {
        // write code here
        ArrayList<ArrayList<Integer>> retList=new ArrayList<>();
        if(ope==null||ope.length==0||size<=0){
            return retList;
        }


        for(int i=0;i<ope.length;i++){
            if(ope[i][0]==1){
                boolean isNew=false;
                ArrayList<Integer> curStack=null;
                if(retList.isEmpty()){
                    curStack=new ArrayList<>();
                    isNew=true;
                }else{
                    curStack=retList.get(retList.size()-1);
                    if(curStack.size()==size){
                        curStack=new ArrayList<>();
                        isNew=true;
                    }
                }

                curStack.add(ope[i][1]);
                if(isNew){
                    retList.add(curStack);
                }
            }else if(ope[i][0]==2){
                if(retList.size()>0){
                    ArrayList<Integer> curStack=retList.get(retList.size()-1);
                    curStack.remove(curStack.size()-1);

                    if(curStack.isEmpty()){
                        retList.remove(curStack);
                    }
                }
            }
        }

        return retList;
    }

}
